﻿using Avalonia.Controls;
using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Threading.Tasks;
using Ursa.Controls;

namespace AvalonAdmin.BaseViewModel
{
    /// <summary>
    /// 基础ViewModel类，提供通用的加载状态管理和通知功能
    /// </summary>
    public abstract class Base : ObservableObject
    {
        #region 加载状态管理

        private bool _isLoading = false;

        /// <summary>
        /// 获取或设置加载状态
        /// </summary>
        public bool IsLoading
        {
            get => _isLoading;
            set => SetProperty(ref _isLoading, value);
        }

        /// <summary>
        /// 设置加载状态并执行异步操作
        /// </summary>
        /// <param name="action">要执行的异步操作</param>
        /// <returns>异步任务</returns>
        protected async Task ExecuteWithLoadingAsync(Func<Task> action)
        {
            if (action == null) return;

            try
            {
                IsLoading = true;
                await action();
            }
            catch (Exception ex)
            {
                ShowError($"操作失败：{ex.Message}");
            }
            finally
            {
                IsLoading = false;
            }
        }

        /// <summary>
        /// 设置加载状态并执行同步操作
        /// </summary>
        /// <param name="action">要执行的同步操作</param>
        protected void ExecuteWithLoading(Action action)
        {
            if (action == null) return;

            try
            {
                IsLoading = true;
                action();
            }
            catch (Exception ex)
            {
                ShowError($"操作失败：{ex.Message}");
            }
            finally
            {
                IsLoading = false;
            }
        }

        #endregion

        #region 通知管理

        /// <summary>
        /// 通知管理器
        /// </summary>
        public WindowNotificationManager? NotificationManager { get; set; }

        /// <summary>
        /// 显示警告通知
        /// </summary>
        /// <param name="content">通知内容</param>
        /// <param name="title">通知标题</param>
        public void ShowWarning(string content, string title = "友情提醒", bool showIcon = true)
        {
            if (string.IsNullOrWhiteSpace(content)) return;

            NotificationManager?.Show(new Notification(title, content),
                    showIcon: showIcon,
                    showClose: true,
                    type: Avalonia.Controls.Notifications.NotificationType.Warning);
        }

        /// <summary>
        /// 显示信息通知
        /// </summary>
        /// <param name="content">通知内容</param>
        /// <param name="title">通知标题</param>
        public void ShowInformation(string content, string title = "信息提醒", bool showIcon = true)
        {
            if (string.IsNullOrWhiteSpace(content)) return;

            NotificationManager?.Show(new Notification(title, content),
                    showIcon: showIcon,
                    showClose: true,
                    type: Avalonia.Controls.Notifications.NotificationType.Information);
        }

        /// <summary>
        /// 显示错误通知
        /// </summary>
        /// <param name="content">通知内容</param>
        /// <param name="title">通知标题</param>
        public void ShowError(string content, string title = "错误提醒", bool showIcon = true)
        {
            if (string.IsNullOrWhiteSpace(content)) return;

            NotificationManager?.Show(new Notification(title, content),
                     showIcon: showIcon,
                     showClose: true,
                     type: Avalonia.Controls.Notifications.NotificationType.Error);
        }

        /// <summary>
        /// 显示成功通知
        /// </summary>
        /// <param name="content">通知内容</param>
        /// <param name="title">通知标题</param>
        public void ShowSuccess(string content, string title = "成功提醒", bool showIcon = true)
        {
            if (string.IsNullOrWhiteSpace(content)) return;

            NotificationManager?.Show(new Notification(title, content),
                      showIcon: showIcon,
                      showClose: true,
                      type: Avalonia.Controls.Notifications.NotificationType.Success);
        }

        #endregion

        #region 消息提示管理

        /// <summary>
        /// 显示普通消息
        /// </summary>
        /// <param name="message"></param>
        protected virtual async Task<MessageBoxResult> ShowMsgInfo(string message, string title = "", MessageBoxIcon messageBoxIcon = MessageBoxIcon.Information, MessageBoxButton messageBoxButton = MessageBoxButton.OK)
        {
            return await MessageBox.ShowOverlayAsync(message, title, icon: messageBoxIcon, button: messageBoxButton);
        }

        /// <summary>
        /// 显示成功消息
        /// </summary>
        /// <param name="message"></param>
        protected virtual async Task<MessageBoxResult> ShowMsgSuccess(string message, string title = "成功", MessageBoxIcon messageBoxIcon = MessageBoxIcon.Success, MessageBoxButton messageBoxButton = MessageBoxButton.OK)
        {
            return await MessageBox.ShowOverlayAsync(message, title, icon: messageBoxIcon, button: messageBoxButton);
        }

        /// <summary>
        /// 显示警告消息
        /// </summary>
        /// <param name="message"></param>
        protected virtual async Task<MessageBoxResult> ShowMsgWarning(string message, string title = "警告", MessageBoxIcon messageBoxIcon = MessageBoxIcon.Warning, MessageBoxButton messageBoxButton = MessageBoxButton.OK)
        {
            return await MessageBox.ShowOverlayAsync(message, title, icon: messageBoxIcon, button: messageBoxButton);
        }

        /// <summary>
        /// 显示错误消息
        /// </summary>
        /// <param name="message"></param>
        protected virtual async Task<MessageBoxResult> ShowMsgError(string message, string title = "错误", MessageBoxIcon messageBoxIcon = MessageBoxIcon.Error, MessageBoxButton messageBoxButton = MessageBoxButton.OK)
        {
           return await MessageBox.ShowOverlayAsync(message, title, icon: messageBoxIcon, button: messageBoxButton);
        }
        #endregion

        #region 虚方法供子类重写

        /// <summary>
        /// 初始化数据，子类可重写此方法
        /// </summary>
        protected virtual void Initialize() { }

        /// <summary>
        /// 清理资源，子类可重写此方法
        /// </summary>
        protected virtual void Cleanup() { }

        #endregion
    }
}
